﻿using System.Linq;
using System;

using Organizer.Model;
using System.Collections.Generic;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;

namespace Organizer.ViewModel
{
    using System.Net;
    using System.Threading;
    using System.Xml.Linq;

    public class ForismaticCitesViewModel : BaseViewModel
    {
        #region constructors

        public ForismaticCitesViewModel()
        {
            this.GetCite = new RelayCommand(() => GetCiteForismatic());
            GetCite.Execute(this);
        }

        #endregion

        public string QuoteText { get; set; }
        public string QuoteAuthor { get; set; }

        public List<Man> Men
        {
            get
            {
                return OrganizerDataContext.Instance.Mans.ToList();
            }
        }

        #region commands

        public ICommand GetCite { get; set; }

        #endregion

        #region methods

        private void GetCiteForismatic()
        {
            this.DownloadString(Thread.CurrentThread.CurrentUICulture.Name.Split('-')[0]);
        }

        private WebClient webClient;

        private void DownloadString(string localization)
        {
            webClient = new WebClient();
            webClient.DownloadStringCompleted += MyHandler;
            webClient.DownloadStringAsync(new Uri(string.Format("http://api.forismatic.com/api/1.0/?method={0}&key={1}&format=xml&lang={2}", "getQuote", new Random().Next(999999), localization)));
        }


        /*<forismatic>
         *   <quote>
         *      <quoteText>
         *          The beginning of wisdom is found in doubting; by doubting we come to the question, and by seeking we may come upon the truth. </quoteText>
         *      <quoteAuthor>
         *          Pierre Abelard
         *      </quoteAuthor>
         *      <senderName></senderName>
         *      <senderLink></senderLink>
         *      <quoteLink>
         *          http://en.forismatic.com/25bb811368/
         *      </quoteLink>
         *   </quote>
         *</forismatic>*/
        private void MyHandler(object sender, DownloadStringCompletedEventArgs e)
        {
            XDocument doc = XDocument.Parse(e.Result);
            if (doc.Root != null)
            {
                this.FindAuthor(doc.Root.Elements());
            }
        }

        private void FindAuthor(IEnumerable<XElement> nodes)
        {
            foreach (var node in nodes)
            {
                foreach (XElement element in node.Elements())
                    switch (element.Name.ToString())
                    {
                        case "quoteAuthor":
                            {
                                if (element.Value != null)
                                {
                                    QuoteAuthor = element.Value;
                                    this.NotifyPropertyChanged(() => this.QuoteAuthor);
                                }
                            }
                            break;
                        case "quoteText":
                            {
                                if (element.Value != null)
                                {
                                    QuoteText = element.Value;
                                    this.NotifyPropertyChanged(() => this.QuoteText);
                                }
                            }
                            break;
                    }
            }
        }

        #endregion
    }
}
